package Tests;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumRegisterTest extends AutoTestUtils {
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        //打开博客登录页面
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击注册按钮
        webDriver.findElement(By.cssSelector("body > div.page.page-center > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a")).click();
        sleep(3000);
    }

    /*
    * 检查注册页是否正确
     */
    @Order(1)
    @Test
    void RegisterPageLoadRight() {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:9580/sign-up.html", cur_url);
    }

    /*
     * 注册账号成功
     */

    @Order(2)
    @ParameterizedTest
    @CsvSource({"admin1,鹿小葵,12345,12345","admin2,文汉天女,123,123"})
    void RegisterSuccess(String username, String nickname, String password, String passwordRepeat) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 在每次注册之后都要进行清空，然后才能重新输入
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#nickname")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#passwordRepeat")).clear();

        //输入用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入昵称
        webDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        //输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //输入确认密码
        webDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);

        //勾选同意按钮
        webDriver.findElement(By.cssSelector("#policy")).click();
        sleep(3000);
        //注册提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);

    }

    /*
     * 检查异常登录情况:
     * 多参数测试：为空 or 非空
     * 登录失败的检查元素和成功是不一样的
     */
    @Order(2)
    @ParameterizedTest
    @CsvSource({"admin1,'',12345,12345","admin2,文汉天女,123,1234"})
    void RegisterFail(String username, String nickname, String password, String passwordRepeat) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 在每次注册之后都要进行清空，然后才能重新输入
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#nickname")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#passwordRepeat")).clear();


        //输入用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入昵称
        webDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        //输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //输入确认密码
        webDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("passwordRepeat");

        try{
            // 等待invalid-feedback元素出现
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#invalid-feedback")));
            System.out.println("invalid-feedback 元素存在");
        } catch (Exception e) {
            System.out.println("invalid-feedback 元素不存在");
        }

        //不勾选同意按钮
        boolean check_agree = webDriver.findElement(By.xpath("//*[@id=\"policy\"]")).isSelected();
        if(check_agree) {
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }
        sleep(3000);


        //确认密码与密码一致,测试通过
        if(password.equals(passwordRepeat)) {
            Assertions.assertEquals(password, passwordRepeat);
        }
        else{
            Assertions.assertNotEquals(password, passwordRepeat);
        }


    }
}
